package character.sprite.gui;

import java.awt.Component;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.TransferHandler;

import character.sprite.model.CharacterStyle;

public class CharacterStyleList extends JList {

	private static final long serialVersionUID = 5542944373451956537L;

	public CharacterStyleList() {
		this.setDragEnabled(true);
		this.setCellRenderer(new StyleRenderer());
		this.setTransferHandler(new StyleListTransferHandler());
	}

	class StyleRenderer extends DefaultListCellRenderer {
		private static final long serialVersionUID = -7707471698938060570L;

		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			Component c = super.getListCellRendererComponent(list, value,
					index, isSelected, cellHasFocus);
			CharacterStyle cs = (CharacterStyle) value;
			((JLabel) c).setIcon(new ImageIcon(cs.getThumbnailImage()));
			((JLabel) c).setText(cs.getData().getDisplayName());
			return c;
		}
	}

	class StyleListTransferHandler extends TransferHandler {

		private static final long serialVersionUID = -5822482582979772714L;

		public int getSourceActions(JComponent c) {
			return COPY;
		}

		protected Transferable createTransferable(JComponent c) {
			JList list = (JList) c;

			String styleGUID = ((CharacterStyle) list.getSelectedValue())
					.getGUID();
			return new StringSelection(styleGUID);
		}

	}

}
